//
//  SLBaseRequest.m
//  shunlianPower2
//
//  Created by 童玉龙 on 2018/12/8.
//  Copyright © 2018 顺联网络. All rights reserved.
//

#import "SLBaseRequest.h"

@implementation SLBaseRequest

-(instancetype)init{
    if(self = [super init]){
        self.responseSerializerType = YTKResponseSerializerTypeJSON;
    }
    return self;
}
- (id)requestArgument {
    return self.parameters;
}

- (NSString *)requestUrl {
    return self.path;
}

- (YTKRequestMethod)requestMethod {
    return self.method;
}
-(YTKRequestSerializerType)requestSerializerType{
    return _requestSerializerType;
}
-(YTKResponseSerializerType)responseSerializerType{
    return _responseSerializerType;
}
-(NSDictionary<NSString *,NSString *> *)requestHeaderFieldValueDictionary{
    return self.requestHeader;
}

+ (SLBaseRequest *)getRequestWithDelegate:(nullable id<SLRequestDelegate>)delegate path:(NSString *)path parameters:(nullable NSDictionary *)parameters {
    return [[self class] requestWithDelegate:delegate method:YTKRequestMethodGET path:path parameters:parameters callBack:nil];
}

+ (SLBaseRequest *)getRequestWithPath:(NSString *)path parameters:(nullable NSDictionary *)parameters callBack:(nullable SLRequestCallBack)callBack {
    return [[self class] requestWithDelegate:nil method:YTKRequestMethodGET path:path parameters:parameters callBack:callBack];
}

+ (SLBaseRequest *)postRequestWithDelegate:(nullable id<SLRequestDelegate>)delegate path:(NSString *)path parameters:(nullable NSDictionary *)parameters {
    return [[self class] requestWithDelegate:delegate method:YTKRequestMethodPOST path:path parameters:parameters callBack:nil];
}

+ (SLBaseRequest *)postRequestWithPath:(NSString *)path parameters:(nullable NSDictionary *)parameters callBack:(nullable SLRequestCallBack)callBack {
    return [[self class] requestWithDelegate:nil method:YTKRequestMethodPOST path:path parameters:parameters callBack:callBack];
}

+ (SLBaseRequest *)requestWithDelegate:(nullable id<SLRequestDelegate>)delegate method:(YTKRequestMethod)method path:(NSString *)path parameters:(nullable NSDictionary *)parameters callBack:(nullable SLRequestCallBack)callBack {
    SLBaseRequest *req = [[[self class] alloc] init];
    req.path = path;
    req.parameters = parameters;
    req.delegate = delegate;
    req.method = method;
    [req startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        SLBaseRequest *currentRequest = (SLBaseRequest *)request;
        currentRequest.responseParsedObject = request.responseJSONObject;
   
        if ([(id <SLRequestDelegate>)currentRequest.delegate respondsToSelector:@selector(request:data:)]) {
            [(id <SLRequestDelegate>)currentRequest.delegate request:currentRequest data:currentRequest.responseParsedObject];
        }
        if (callBack) {
            callBack((SLBaseRequest *)request,currentRequest.responseParsedObject == nil ? @{} : currentRequest.responseParsedObject);
        }
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        if (callBack) {
            callBack((SLBaseRequest *)request,nil);
        }
    }];
    return req;
}

@end
